package com.cskaoyan.catmall.controller.wx;

import com.cskaoyan.catmall.model.bo.BaseParamBo;
import com.cskaoyan.catmall.model.bo.WxCouponMyListBo;
import com.cskaoyan.catmall.model.pojo.MarketUser;
import com.cskaoyan.catmall.model.vo.BaseRespVo;
import com.cskaoyan.catmall.model.vo.WxCouponListVo;
import com.cskaoyan.catmall.model.vo.WxCouponMyListVo;
import com.cskaoyan.catmall.model.vo.WxCouponSelectListVo;
import com.cskaoyan.catmall.service.WxCouponService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 创建日期: 2022-01-10-16-57
 * author : WQW
 * description: 用户优惠卷相关的增删改查
 */

@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {


    @Autowired
    WxCouponService wxCouponService;

    /**
     * @param
     * @param wxcouponMyListBo
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 获取用户自己的优惠卷列表
     * @author WQW
     * @date 2022/1/10 21:48
     */
    @GetMapping("mylist")
    public BaseRespVo myList(WxCouponMyListBo wxcouponMyListBo) {
        MarketUser user = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        if (user==null){
           return BaseRespVo.fail("请先登录");
        }
        WxCouponMyListVo wxCouponMyListVo = wxCouponService.getUserCoupon(wxcouponMyListBo, user.getId());
        return BaseRespVo.ok(wxCouponMyListVo);
    }

    /**
     * @param
     * @param baseParamBo
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 获取所有的用户可领取的优惠卷列表
     * @author WQW
     * @date 2022/1/10 21:49
     */
    @GetMapping("list")
    public BaseRespVo list(BaseParamBo baseParamBo) {
        WxCouponListVo wxCouponListVo = wxCouponService.getAllCoupon(baseParamBo);
        return BaseRespVo.ok(wxCouponListVo);
    }


    /**
     * @param
     * @param couponId
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 用户领取优惠卷
     * @author WQW
     * @date 2022/1/10 21:49
     */
    @PostMapping("receive")
    public BaseRespVo receive(@RequestBody Map<String, Integer> map) {
        MarketUser user = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        Integer couponId = map.get("couponId");
        if (user == null){
            return BaseRespVo.fail("请先登录");
        }
        int result = wxCouponService.userGetCoupon(couponId, user.getId());
        if (result == 101) {
            return BaseRespVo.fail("超出限领次数");
        } else if (result == 102) {
            return BaseRespVo.fail("优惠卷已经领取完");
        }
        return BaseRespVo.ok("领取成功");
    }


    /**
     * @param
     * @param cartId
     * @param grouponRulesId
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     * @description 用户下单时获取可以使用的优惠卷列表
     * @author WQW
     * @date 2022/1/11 9:48
     */
    @GetMapping("selectlist")
    public BaseRespVo selectList(Integer cartId, Integer grouponRulesId) {
        MarketUser user = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        WxCouponSelectListVo wxCouponSelectListVo = wxCouponService.getUserAvailableCoupon(user.getId());
        return BaseRespVo.ok(wxCouponSelectListVo);
    }


    /**
     * @description 使用兑换码兑换优惠卷
     * @author WQW
     * @date 2022/1/11 10:59
     * @param
     * @param map
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     */
    @PostMapping("exchange")
    public BaseRespVo exchange(@RequestBody Map<String,String> map){
        MarketUser user = (MarketUser) SecurityUtils.getSubject().getPrincipal();
        int result = wxCouponService.exchangeCoupon(map.get("code"),user.getId());
        if (result == 104){
            return BaseRespVo.fail("兑换码已经过期");
        }else if (result == 103){
            return BaseRespVo.fail("兑换码不正确");
        }else if (result == 102){
            return BaseRespVo.fail("优惠卷已经领取完");
        }else if (result == 101){
            return BaseRespVo.fail("超出限领次数");
        }
        return BaseRespVo.ok("领取成功");
    }

}
